package com.jzo2o.orders.manager.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.orders.base.enums.OrderCancelStatusEnum;
import com.jzo2o.orders.base.enums.OrderRefundStatusEnum;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.mapper.OrdersCanceledMapper;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.base.model.domain.OrdersCanceled;
import com.jzo2o.orders.manager.service.IOrdersCanceledService;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * <p>
 * 订单取消服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-08-19
 */
@Service
@Slf4j
public class OrdersCanceledServiceImpl extends ServiceImpl<OrdersCanceledMapper, OrdersCanceled> implements IOrdersCanceledService {

    @Resource
    private IOrdersManagerService ordersManagerService;

    @Override
    public void CancelOrderByMq(Long id) {
        log.info("订单到达取消时间");
        OrdersCanceled ordersCanceled = new OrdersCanceled();
        ordersCanceled.setId(id);
        ordersCanceled.setCancellerType(OrderCancelStatusEnum.ADMIN.getStatus());
        ordersCanceled.setCancelReason("到达取消时间自动取消");
        ordersCanceled.setCancelTime(LocalDateTime.now());
        this.save(ordersCanceled);

    }

    /**
     * 取消订单
     *
     * @param orderId      用户id
     * @param cancelReason 取消原因
     */
    @Override
    public void cancelOrder(Long orderId, String cancelReason) {
        log.info("用户取消订单");
        OrdersCanceled ordersCanceled = new OrdersCanceled();
        ordersCanceled.setId(orderId);
        ordersCanceled.setCancellerId(UserContext.currentUserId());
        ordersCanceled.setCancelReason(cancelReason);
        ordersCanceled.setCancellerType(OrderCancelStatusEnum.USER.getStatus());
        ordersCanceled.setCancelTime(LocalDateTime.now());
        ordersCanceled.setCreateTime(LocalDateTime.now());
        ordersCanceled.setUpdateTime(LocalDateTime.now());
        this.saveOrUpdate(ordersCanceled);
    }

    /**
     * 退款成功
     *
     * @param refundInfo 退款信息
     */
    @Override
    public void refundSuccess(String refundInfo) {
        /*refundMap.put("refundId", transaction.getRefundId());
        refundMap.put("refundNo", transaction.getOutRefundNo());*/

        HashMap bean = JSONUtil.toBean(refundInfo, HashMap.class);
        String refundId = (String) bean.get("refundId");
        String refundNo = (String) bean.get("refundNo");
        String tradingNo = (String) bean.get("tradingNo");
        //修改订单信息
        Orders orders = ordersManagerService.getOne(Wrappers.<Orders>lambdaQuery().eq(Orders::getTradingOrderNo, tradingNo));
        if (orders == null) {
            log.error("订单不存在");
            throw new CommonException("订单不存在");
        }
        log.info("消息解析成功，开始修改订单信息");
        orders.setRefundNo(Long.valueOf(refundNo));
        orders.setRefundId(refundId);
        orders.setRefundStatus(OrderRefundStatusEnum.REFUND_SUCCESS.getStatus());
        orders.setOrdersStatus(OrderStatusEnum.CANCELED.getStatus());
        ordersManagerService.updateById(orders);
    }
}
